package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.MemberEntity;
import io.renren.entity.TeamDto;
import io.renren.service.MemberService;
import io.renren.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 团队相关
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/team")
@Api(tags = "团队接口")
public class ApiTeamController extends AbstractController {

	@Autowired
	private TeamService teamService;

	@Autowired
	MemberService memberService;

	@Login
	@PostMapping("info")
	@ApiOperation("团队信息")
	public R info(@ApiIgnore @RequestAttribute("userId") Long userId) {
		TeamDto team = teamService.countTeamByMemberId(userId);
		return R.ok().put("teamAll", team.getTeamAll()).put("teamHashSecond", team.getTeamHashSecond());
	}

	@Login
	@PostMapping("oneList")
	@ApiOperation("直推会员")
	public R oneList(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("level", 1);
		params.put("id", userId);
		PageUtils page = teamService.countTeamByHigher(params);
		setMemberInfo(page.getList());
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("twoList")
	@ApiOperation("二代会员")
	public R twoList(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("level", 2);
		params.put("id", userId);
		PageUtils page = teamService.countTeamByHigher(params);
		setMemberInfo(page.getList());
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("threeList")
	@ApiOperation("三代会员")
	public R threeList(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("level", 3);
		params.put("id", userId);
		PageUtils page = teamService.countTeamByHigher(params);
		setMemberInfo(page.getList());
		return R.ok().put("page", page);
	}

	private void setMemberInfo(List<?> list) {
		if (CollectionUtils.isNotEmpty(list)) {
			for (Object o : list) {
				TeamDto dto = (TeamDto) o;
				MemberEntity member = memberService.selectById(dto.getMemberId());
				try {
					dto.setNickname(member.getMobile() + (StringUtils.isNotBlank(member.getRealname()) ? "(" + member.getRealname() + ")" : ""));
				} catch (Exception e) {
					logger.error(e.getMessage());
				}
				dto.setUserLevel(member.getUserLevel());
				dto.setHeadImg(member.getHeadImg());
			}
		}
	}
}
